﻿using System;
using System.Windows;
using System.Globalization;
using System.Runtime.Serialization;

namespace CI.MesopotamiaScrnSaver
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            MiniLogger.LogDebug("OnStartup");

            var args = e.Args;
            if (args.Length > 0)
            {
                // Get the 2 character command line argument
                var arg = args[0].ToLower(CultureInfo.InvariantCulture).Trim().Substring(0, 2);
                switch (arg)
                {
                    case "/c":
                        // Show the options dialog
                        //Settings settings = new Settings();
                        //settings.Show();
                        break;
                    case "/p":
                        // Don't do anything for preview
                        Shutdown();
                        break;
                    case "/s":
                        // Show screensaver form
                        ShowScreensaver();
                        break;
                    default:
                        Shutdown();
                        break;
                }
            }
            else
            {
                // If no arguments were passed in, show the screensaver
                ShowScreensaver();
            }
        }

        /// <summary>
        /// Shows the screen-saver by creating an instance on Main.xaml per screen.
        /// </summary>
        private void ShowScreensaver()
        {

            try
            {
                var mesFiles = new MesoptamiaFiles();

                // load the config
                using (System.IO.Stream sw = new System.IO.FileStream(System.IO.Path.Combine(Environment.SystemDirectory, ScreenSaverInstaller.ConfigFile), System.IO.FileMode.Open, System.IO.FileAccess.Read))
                {
                    var dataContractSerializer = new DataContractSerializer(mesFiles.GetType());
                    mesFiles = (MesoptamiaFiles)dataContractSerializer.ReadObject(sw);
                }

                // launch the process
                var mesopotamia = new System.Diagnostics.Process
                                      {
                                          StartInfo =
                                              {
                                                  WorkingDirectory = mesFiles.MesopotamiaPath,
                                                  FileName =
                                                      System.IO.Path.Combine(mesFiles.MesopotamiaPath,
                                                                             mesFiles.MesopotamiaExecutable)
                                              }
                                      };

                MiniLogger.LogDebug("Working Directory : " + mesFiles.MesopotamiaPath);
                MiniLogger.LogDebug("Exectutable : " + mesFiles.MesopotamiaExecutable);

                mesopotamia.StartInfo.UseShellExecute = false;
                mesopotamia.Start();
                mesopotamia.WaitForExit();
            }
            catch (Exception ex)
            {
                MiniLogger.LogDebug("Error starting screen saver : " + ex.Message);
                throw;
            }
            finally
            {
                Shutdown();
            }


        }
    }

    [DataContract]
    public class MesoptamiaFiles
    {
        [DataMember]
        public string MesopotamiaExecutable;
        [DataMember]
        public string MesopotamiaPath;

        public override string ToString()
        {
            var result = "";

            result += "Path : " + MesopotamiaPath;
            result += " Exectuable : " + MesopotamiaExecutable;

            return result;
        }
    }
}
